using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QLNhaSach.BUS.DTO;

namespace QLNhaSach.DAO
{
    public class HoaDonDAO
    {
        //Hien thi danh sach tat ca cac hoa don
        public static DataTable DSHD()
        {
            string sql = "SELECT * FROM HoaDon hd,KhachHang kh, NhanVien nv where hd.MaKH = kh.MaKH and hd.MaNV = nv.MaNV";
            return Config.AccessHelper.ExecuteQuery(sql);

        }
        
        //Lay 1 hoa don cua 1 khach hang
        public static DataTable LayHDKH(HoaDonDTO hd)
        {
            string sql = "SELECT * FROM HoaDon where MaHD=" + hd.MaHD;
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        //Load tat ca hoa don cua 1 khach hang
        public static DataTable HDMotKH(KhachHangDTO kh)
        {
            string sql = "SELECT *FROM HoaDon where MaKH=" + kh.MaKH;
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        //Them mot hoa don vao csdl
        public static bool InsertHD(HoaDonDTO hd)
        {
            try
            {
                string sql = "INSERT INTO HoaDon (NgayCap, MaKH, MaNV, Loai) ";
                sql += " VALUES ('" + hd.NgayCap + "' , " + hd.MaKH + ", " + hd.MaNV + ", 1)";
                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Lay ma hoa don vua them vao csdl
        public static DataTable LayMaHD()
        {
            string sql = "SELECT * FROM HoaDon";
            return Config.AccessHelper.ExecuteQuery(sql);
        }

        //Xoa mot hoa don
        public static bool XoaHoaDon(HoaDonDTO hd)
        {
            try
            {
                string sql = "DELETE FROM HoaDon WHERE (MaHD = " + hd.MaHD + ")";
                Config.AccessHelper.ExecuteNonQuery(sql);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
